capture log close
log using $log/AED_dataprep_07_xvars, text replace
   
clear
set mem 8000m
set matsize 800
set more off


   
/* ------------------------------------------------------------------------------- */
/* --- Define control variables measured at entry into unemployment or before ---- */
/* ------------------------------------------------------------------------------- */

use $orig/AED_9310_v1_bhp_basis_v1, clear
keep jahr betnr w03_3
drop if jahr<2003
drop if jahr>2008

rename betnr last_betnr

*classification of economic activities based on variable w03

gen sector_w03=1 if w03_3>=11 & w03_3<=50
replace sector_w03=2 if w03_3>=101 & w03_3<=145
replace sector_w03=3 if w03_3>=151 & w03_3<=372
replace sector_w03=4 if w03_3>=401 & w03_3<=410
replace sector_w03=5 if w03_3>=451 & w03_3<=455
replace sector_w03=6 if w03_3>=501 & w03_3<=527
replace sector_w03=7 if w03_3>=551 & w03_3<=555
replace sector_w03=8 if w03_3>=601 & w03_3<=643
replace sector_w03=9 if w03_3>=651 & w03_3<=672
replace sector_w03=10 if w03_3>=701 & w03_3<=748
replace sector_w03=11 if w03_3>=751 & w03_3<=753
replace sector_w03=12 if w03_3>=801 & w03_3<=804
replace sector_w03=13 if w03_3>=851 & w03_3<=853
replace sector_w03=14 if w03_3>=900 & w03_3<=930
replace sector_w03=15 if w03_3>=950 & w03_3<=954
replace sector_w03=16 if w03_3==990	
			
gen sector_agg=1 if sector_w03==1 /*Agriculture*/
replace sector_agg=2 if (sector_w03==2|sector_w03==5) /* Construction, mining*/
replace sector_agg=3 if sector_w03==3 /*Manufacturing*/
replace sector_agg=4 if (sector_w03==4|sector_w03==6|sector_w03==8) /*Trade, logistic, infrastructure*/
replace sector_agg=5 if ((sector_w03>=7 & sector_w03<=10)|sector_w03==15) /*Private sector services*/
replace sector_agg=6 if sector_agg==. /*Public sector services, others*/

tempfile sector_agg
save `sector_agg'
			

foreach iebversion in v901 {

	forvalues year=2005/2008 {


		use $data/eintritte_ue_`year'_`iebversion', clear
		rename iza_id persnr
		
		/*merge raw data*/		
		joinby persnr using $orig/AED_9310_v1, unmatched(master)
		tab _merge
		drop _merge
		
		rename persnr iza_id
		rename begorig beg_orig
		rename endorig end_orig
		rename betnr bet_nr
		
		sort iza_id beg_orig
		
		by iza_id: drop if beg_orig > iza_eintrittd
		drop if iza_eintrittd==.
		/*drop information after entry into ue*/
		
		
		
		
			rename gebjahr geb_jahr
			rename estatvor zug_gr
			
			rename nation_gr staat
			rename bild ausbildung
			rename tentgelt tag_entg
			
			#delimit ; 
		
			local variable	
			beruf ausbildung schbild staat traeger wo_kreis zug_gr
			grund art_kuend beruf_ausbild  gebjahr_jkind kind famst tag_entg
			w93_3_vor quali berufserfahrung anz_beratung anz_verm restanspruch
			;
			#delimit cr
			sort iza_id beg_orig
			
			
			/*=== Variables from BEH ======================== */

			gen geb_jahr0  = gebjahr		

			by iza_id: gen     zug_gr_temp = zug_gr
			by iza_id: replace zug_gr_temp = zug_gr_temp[_n-1] if  zug_gr_temp==.
			by iza_id: gen     zug_gr0     = zug_gr_temp[_N]
			drop zug_gr_temp
		
			by iza_id: gen     wo_kreis_temp = wo_kreis
			by iza_id: replace wo_kreis_temp = wo_kreis_temp[_n-1] if wo_kreis==.
			by iza_id: gen     wo_kreis0     = wo_kreis_temp[_N]
			drop wo_kreis_temp
			
			by iza_id: gen     traeger_temp = traeger
			by iza_id: replace traeger_temp = traeger_temp[_n-1] if traeger==.
			by iza_id: gen     traeger0     = traeger_temp[_N]
			drop traeger_temp
		
			by iza_id: gen     staat_temp = staat
			by iza_id: replace staat_temp = staat_temp[_n-1] if staat==.
			by iza_id: gen     staat0     = staat_temp[_N]
			drop staat_temp
							
			by iza_id: egen schbild0 = max(schbild)

			by iza_id: egen bild_beh = max(ausbildung) if quelle == 1
			
			replace ausbildung=. if quelle==1
			by iza_id: gen     ausbildung_temp = ausbildung
			by iza_id: replace ausbildung_temp = ausbildung_temp[_n-1] if ausbildung==.
			by iza_id: gen     ausbildung0     = ausbildung_temp[_N]
			drop ausbildung_temp	
		
			by iza_id: gen beruf_temp     = beruf
			by iza_id: replace beruf_temp = beruf_temp[_n-1] if beruf_temp==.
			by iza_id: gen beruf0         = beruf_temp[_N] 
			drop beruf_temp
			
			by iza_id: gen     tag_entg_help = tag_entg if quelle==1
			by iza_id: gen     tag_entg_temp = tag_entg if quelle==1
			by iza_id: replace tag_entg_temp = tag_entg_temp[_n-1] if tag_entg_help==.
			by iza_id: gen     tag_entg0     = tag_entg_temp[_N]
			drop tag_entg_temp tag_entg_help
		
		
			/* === Variables from ASU ======================== */
		
			by iza_id: gen     famst_temp = famst
			by iza_id: replace famst_temp = famst_temp[_n-1] if famst==.
			by iza_id: gen     famst0     = famst_temp[_N]
			drop famst_temp
			
			by iza_id: gen     kind_temp = kind
			by iza_id: replace kind_temp = kind_temp[_n-1] if kind==.
			by iza_id: gen     kind0     = kind_temp[_N]
			drop kind_temp
		
			by iza_id: gen     gebjahr_jkind_temp = gebjahr_jkind
			by iza_id: replace gebjahr_jkind_temp = gebjahr_jkind_temp[_n-1] if gebjahr_jkind==.
			by iza_id: gen     gebjahr_jkind0     = gebjahr_jkind_temp[_N]
			drop gebjahr_jkind_temp

			by iza_id: gen     beruf_ausbild_temp = beruf_ausbild
			by iza_id: replace beruf_ausbild_temp = beruf_ausbild_temp[_n-1] if beruf_ausbild==.
			by iza_id: gen     beruf_ausbild0     = beruf_ausbild_temp[_N]
			drop beruf_ausbild_temp
				
			by iza_id: gen     art_kuend_temp = art_kuend
			by iza_id: replace art_kuend_temp = art_kuend_temp[_n-1] if art_kuend==.
			by iza_id: gen     art_kuend0     = art_kuend_temp[_N]
			drop art_kuend_temp
				
			by iza_id: gen     grund_temp = grund
			by iza_id: replace grund_temp = grund_temp[_n-1] if grund==.
			by iza_id: gen     grund0     = grund_temp[_N]
			drop grund_temp
					
			by iza_id: gen     w93_3_vor_temp = w93_3_vor
			by iza_id: replace w93_3_vor_temp = w93_3_vor_temp[_n-1] if w93_3_vor==.
			by iza_id: gen     w93_3_vor0     = w93_3_vor_temp[_N]
			drop w93_3_vor_temp
		
			by iza_id: gen     quali_temp = quali
			by iza_id: replace quali_temp = quali_temp[_n-1] if quali==.
			by iza_id: gen     quali0     = quali_temp[_N]
			drop quali_temp	
		
			by iza_id: gen     berufserfahrung_temp = berufserfahrung
			by iza_id: replace berufserfahrung_temp = berufserfahrung_temp[_n-1] if berufserfahrung==.
			by iza_id: gen     berufserfahrung0     = berufserfahrung_temp[_N]
			drop berufserfahrung_temp	
			
			by iza_id: gen     anz_beratung_temp = anz_beratung
			by iza_id: replace anz_beratung_temp = anz_beratung_temp[_n-1] if anz_beratung==.
			by iza_id: gen     anz_beratung0     = anz_beratung_temp[_N]
			drop anz_beratung_temp	
			
			by iza_id: gen     anz_verm_temp = anz_verm
			by iza_id: replace anz_verm_temp = anz_verm_temp[_n-1] if anz_verm==.
			by iza_id: gen     anz_verm0     = anz_verm_temp[_N]
			drop anz_verm_temp	

			by iza_id: gen restanspruch_temp = restanspruch + (end_orig-beg_orig) if iza_eintrittd== beg_orig & quelle==2
			by iza_id: egen restanspruch0 = min(restanspruch_temp)
			drop restanspruch_temp
			
			gen iza_empl = 0
				#delimit;
				by iza_id: replace iza_empl = 1 if quelle==1 & (erwstat==101 |
							       erwstat==102 |
							       erwstat==103 | 
							       erwstat==105 | 
							       erwstat==106 |
							       erwstat==112 | 
							       erwstat==118 | 
							       erwstat==119 | 
							       erwstat==120 | 
							       erwstat==140 |
							       erwstat==141 | 
							       erwstat==142 | 
							       erwstat==143 | 
							       erwstat==149 | 
							       erwstat==201 | 
							       erwstat==203 |
							       erwstat==205 )
					;
			#delimit cr
			
			rename erwstat erw_stat	
			by iza_id: gen abm_help=1 if quelle==3
			by iza_id: gen abm_beg_help= beg_orig if abm_help==1
			by iza_id: gen abm_end_help= end_orig if abm_help==1
				
			forvalue x = 1/12 {
				
			by iza_id: egen abm_beg`x'=min(abm_beg_help)
			by iza_id: egen abm_end`x'=min(abm_end_help)
			by iza_id: replace iza_empl=0 if beg_orig>=abm_beg`x' & end_orig<=abm_end`x'
			by iza_id: replace abm_beg_help=. if abm_beg_help==abm_beg`x'
			by iza_id: replace abm_end_help=. if abm_end_help==abm_end`x'
				
			}
			count if abm_beg_help~=.
			error r(N)~=0
			drop abm*
			
			
			/*Establishment number last job*/
			gen last_begorig_temp=beg_orig if bet_nr~=.n & bet_nr~=.
			by iza_id: egen last_begorig=max(last_begorig_temp)
			gen last_betnr_temp=bet_nr if beg_orig==last_begorig
			
			gen jahr= .
			forvalue help = 1/8 {
			replace jahr = 200`help' if last_begorig<= d(31dec200`help') & last_begorig>= d(1jan200`help')
			}
			by iza_id: egen last_betnr=max(last_betnr_temp)
			format last_betnr %10.0f
			
			*Merge sector of last job
			joinby last_betnr jahr using `sector_agg',unmatched(master)
			tab _merge
			drop _merge
			
			
			/*-------------------------------------------------------*/
			/*-------- Define new set of variables ------------------*/
			/*-------------------------------------------------------*/
			
			gen alo_leh=1 if (quelle==2 & erw_stat==1)
			replace alo_leh=1 if (quelle==2 & erw_stat==2)
			
			/*Time since last employment if in last 24 months*/
			by iza_id: egen time_sv_temp=max(end_orig) if (iza_empl==1 & beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd)
			by iza_id: gen time_sv_help=iza_eintrittd - time_sv_temp
			by iza_id: egen iza_time_sv=max(time_sv_help)
			drop time_sv_temp time_sv_help
			
			/*Massnahmen Definition*/
			#delimit;
			gen mth_help=1 if (quelle==3 | quelle==4 | quelle==5 | quelle==6 | quelle==8 | quelle==9 | quelle==10
			| quelle==11 | quelle==12 | quelle==13 | quelle==14 | quelle==15 | quelle==17
			| quelle==21| quelle==22 | quelle==24 | quelle==25 | quelle==26 | quelle==27
			| quelle==28 | quelle==29 |quelle==30);
			#delimit cr
			
			/*Any program in last 24 months*/
			by iza_id: gen mth_temp24=1 if (beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd & mth_help==1)
			by iza_id: egen iza_mth24=max(mth_temp24)
			by iza_id: replace iza_mth24=0 if iza_mth24==.
			
			/*Number of programs in last 24 months*/
			by iza_id: gen mth_help24=1 if (beg_orig>= iza_eintrittd-730 & beg_orig<iza_eintrittd & mth_help==1 & erw_stat[_n]~=erw_stat[_n-1])
			by iza_id: replace mth_help24=1 if mth_help==1 & _n==1 & beg_orig>=iza_eintrittd-730
			by iza_id: egen iza_n_mth24=sum(mth_help24)
			drop mth_help24 mth_temp24
			
			/*Any program in last 4 years*/
			by iza_id: gen mth_temp4=1 if (beg_orig>=iza_eintrittd-1460 & beg_orig<iza_eintrittd & mth_help==1)
			by iza_id: egen iza_mth4=max(mth_temp4)
			by iza_id: replace iza_mth4=0 if iza_mth4==.
			
			/*Number of programs in last 4 years*/
			by iza_id: gen mth_help4=1 if (beg_orig>= iza_eintrittd-1460 & beg_orig<iza_eintrittd & mth_help==1 & erw_stat[_n]~=erw_stat[_n-1])
			by iza_id: replace mth_help4=1 if mth_help==1 & _n==1 & beg_orig>=iza_eintrittd-1460
			by iza_id: egen iza_n_mth4=sum(mth_help4)
			drop mth_help4 mth_temp4
			
			/*Any program in last 10 years*/
			by iza_id: gen mth_temp10=1 if (beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd & mth_help==1)
			by iza_id: egen iza_mth10=max(mth_temp10)
			by iza_id: replace iza_mth10=0 if iza_mth10==.
			
			/*Number of programs in last 10 months*/
			by iza_id: gen mth_help10=1 if (beg_orig>= iza_eintrittd-3650 & beg_orig<iza_eintrittd & mth_help==1 & erw_stat[_n]~=erw_stat[_n-1])
			by iza_id: replace mth_help10=1 if mth_help==1 & _n==1 & beg_orig>=iza_eintrittd-3650
			by iza_id: egen iza_n_mth10=sum(mth_help10)
			drop mth_help10 mth_temp10 mth_help
			
			/*Number unemployment spells last 24 months*/
			by iza_id: gen alo24 = 1 if (beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd & alo_leh==1)
			by iza_id: gen alo_help = 1 if alo24[_n+1]==. & alo24[_n]==1
			by iza_id: replace alo_help = 1 if alo24[_n]==1 & alo24[_n]==_N
			by iza_id: egen iza_n_alo24= sum (alo_help)
			by iza_id: replace iza_n_alo24= 0 if iza_n_alo24==.
			drop alo_help alo24
			
			/*Number unemployment spells last 10 years*/
			by iza_id: gen alo10 = 1 if (beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd & alo_leh==1 )
			by iza_id: gen alo_help = 1 if alo10[_n+1]==. & alo10[_n]==1
			by iza_id: replace alo_help = 1 if alo10[_n]==1 & alo10[_n]==_N
			by iza_id: egen iza_n_alo10= sum (alo_help)
			by iza_id: replace iza_n_alo10= 0 if iza_n_alo10==.
			drop alo_help alo10
			
			/*Cumulated duration of UI 4 years before*/
			by iza_id: gen alo4 = 0 if beg_orig>=iza_eintrittd-1460
			by iza_id: replace alo4 = 1 if (quelle==2 & (erw_stat==1 | erw_stat==2)) & beg_orig>=iza_eintrittd-1460
			by iza_id: gen time_help = end_orig-beg_orig if alo4==1
			by iza_id: egen iza_time_alo4 = sum(time_help)
			drop time_help alo4
			
			/*Benefits*/
			gen ben_help=tag_entg if (alo_leh==1 & beg_orig==iza_eintrittd)
			by iza_id: egen iza_ben=max(ben_help)
			drop ben_help
			
			/*Number employers last 24 months*/
			gen help=bet_nr if (beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd)
			replace help=help[_n-1] if help==. & (beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd)			
			sort iza_id help
			gen help2=1 if (help[_n]~=help[_n-1] & help[_n-1]~=. & help[_n]~=. & beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd)
			by iza_id:gen iza_n_ag24=sum(help2)+1 
			by iza_id: replace iza_n_ag24=0 if iza_n_ag24==.
			sort iza_id beg_orig
			drop help help2
			
			/*Number employers last 10 years*/
			gen help=bet_nr if (beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)
			replace help=help[_n-1] if help==. & (beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)			
			sort iza_id help
			gen help2=1 if (help[_n]~=help[_n-1] & help[_n-1]~=. & help[_n]~=. & beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)
			by iza_id:gen iza_n_ag10=sum(help2)+1
			by iza_id: replace iza_n_ag10=0 if iza_n_ag10==.
			sort iza_id beg_orig
			drop help help2
			
			
			/*Full-time/part-time last job*/
			bys iza_id: egen ind_last_temp=max(beg_orig) if quelle==1 & beg_orig<iza_eintrittd
			gen erwstat_last_temp=stib if beg_orig==ind_last_temp & quelle==1
			gen vztz_last_temp=1 if (erwstat_last_temp==7|erwstat_last_temp==8|erwstat_last_temp==9)
			replace vztz_last_temp=0 if vztz_last_temp==.
			bys iza_id: egen iza_vztz_last=max(vztz_last_temp)
			drop ind_last_temp erwstat_last_temp vztz_last_temp
			
			/*Duration last job*/
			gen help=1 if (bet_nr[_n]~=bet_nr[_n-1] & beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)
			by iza_id: egen help2=max(beg_orig) if help==1
			by iza_id: gen help3 = iza_eintrittd-help2
			by iza_id: egen iza_zeit_la=max(help3) 
			drop help help2 help3
			
			/*Time with same employer as last job during the last 10 years*/
			gen help=1 if (bet_nr[_n]~=bet_nr[_n-1] & iza_empl==1 & beg_orig<iza_eintrittd)
			by iza_id: egen help2=max(beg_orig) if help==1
			by iza_id: gen help3=bet_nr if beg_orig==help2
			by iza_id: gen help4=end_orig - beg_orig if (bet_nr==help3 & beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)			
			by iza_id: egen iza_tzeit_la = sum(help4)
			drop help help2 help3 help4
			
			/*Employed 4 years before*/
			gen help_empl=iza_empl if (beg_orig<=iza_eintrittd-1460 & end_orig>=iza_eintrittd-1460)
			by iza_id: egen iza_empl_tminus4=max(help_empl)
			replace iza_empl_tminus4=0 if iza_empl_tminus4==.
			drop help_empl
			
			/*Income 4 years before*/
			gen help_entg=tag_entg if iza_empl==1 & beg_orig<=iza_eintrittd-1460 & end_orig>=iza_eintrittd-1460
			by iza_id: egen iza_entg_tminus4=max(help_entg)
			by iza_id: replace iza_entg_tminus4=0 if iza_entg_tminus4==.
			drop help_entg
			
			/*Cumulated earnings last 4 years*/
			gen entg_help= tag_entg*(end_orig-beg_orig) if (iza_empl==1 & beg_orig>iza_eintrittd-1460 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_income_tminus4=sum(entg_help)
			by iza_id: replace iza_income_tminus4=0 if iza_income_tminus4==.
			drop entg_help
			
			/*Cumulated benefits last 4 years*/
			gen help_entg=tag_entg*(end_orig-beg_orig) if (alo_leh==1 & beg_orig>=iza_eintrittd-1460 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_cum_alog=sum(help_entg)
			by iza_id: replace iza_cum_alog=0 if iza_cum_alog==.
			drop help_entg
			
			/*Time in UE in last 12 months*/
			gen help_alo=end_orig-beg_orig if (alo_leh==1 & beg_orig>=iza_eintrittd-365 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_alo12=sum(help_alo)
			by iza_id: replace iza_alo12=0 if iza_alo12==.
			drop help_alo
			
			/*Time in UE in last 24 months*/
			gen help_alo=end_orig-beg_orig if (alo_leh==1 & beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_alo24=sum(help_alo)
			by iza_id: replace iza_alo24=0 if iza_alo24==.
			drop help_alo
			
			/*Time in UE in last 10 years*/
			gen help_alo=end_orig-beg_orig if (alo_leh==1 & beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_alo10=sum(help_alo)
			by iza_id: replace iza_alo10=0 if iza_alo10==.
			drop help_alo
			
			/*Time in employment in last 12 months*/
			gen help_empl=end_orig-beg_orig if (iza_empl==1 & beg_orig>=iza_eintrittd-365 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_time_empl=sum(help_empl)
			by iza_id: replace iza_time_empl=0 if iza_time_empl==.
			drop help_empl
			
			/*Time in employment in last 24 months*/
			gen help_empl_1=end_orig-beg_orig if (iza_empl==1 & beg_orig>=iza_eintrittd-730 & beg_orig<iza_eintrittd)
			
			by iza_id: egen iza_time_empl2=sum(help_empl_1)
			
			by iza_id: replace iza_time_empl2=0 if iza_time_empl2==.
			drop help_empl_1
			
			/*Time in employment in last 4 years*/
			gen help_empl=end_orig-beg_orig if (iza_empl==1 & beg_orig>=iza_eintrittd-1460 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_time_emp4=sum(help_empl)
			by iza_id: replace iza_time_emp4=0 if iza_time_emp4==.
			drop help_empl
			
			/*Time in employment in last 10 years*/
			gen help_empl=end_orig-beg_orig if (iza_empl==1 & beg_orig>=iza_eintrittd-3650 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_time_empl0=sum(help_empl)
			by iza_id: replace iza_time_empl0=0 if iza_time_empl0==.
			drop help_empl
			
			/*Average earnings last 10 years*/
			gen entg_help= tag_entg*(end_orig-beg_orig) if (iza_empl==1 & beg_orig>iza_eintrittd-3650 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_tot_income=sum(entg_help)
			by iza_id: replace iza_tot_income=0 if iza_tot_income==.
			gen iza_av_income=iza_tot_income/3650
			drop entg_help
			
			/*Cumulated earnings last 12 months*/
			gen entg_help= tag_entg*(end_orig-beg_orig) if (iza_empl==1 & beg_orig>iza_eintrittd-365 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_income_tminus1=sum(entg_help)
			by iza_id: replace iza_income_tminus1=0 if iza_income_tminus1==.
			drop entg_help
			
			/*Cumulated earnings last 24 months*/
			gen entg_help= tag_entg*(end_orig-beg_orig) if (iza_empl==1 & beg_orig>iza_eintrittd-730 & beg_orig<iza_eintrittd)
			by iza_id: egen iza_income_tminus2=sum(entg_help)
			by iza_id: replace iza_income_tminus2=0 if iza_income_tminus2==.
			drop entg_help
			
		
			by iza_id: drop if _n>1
			/* Reduce Data */
		
		
			foreach var in `variable'  {
				replace `var' = `var'0
				drop `var'0
			}
			
			
			

			/* ----------------------------------------------- */
			/* --- Define categories ------------------------- */
			/* ----------------------------------------------- */
			
			
			
			/*Age categories*/
			gen iza_alter = iza_eintritty - geb_jahr
			gen iza_alter_kat = 0 if iza_alter<25
			replace iza_alter_kat = 1 if iza_alter<35 & iza_alter>=25
			replace iza_alter_kat = 2  if iza_alter<45 & iza_alter>=35
			replace iza_alter_kat = 3  if iza_alter<56 & iza_alter>=45
			replace iza_alter_kat = 4  if iza_alter>=56 & iza_alter~=.
		
			/*Marital status*/
			gen iza_famstd = 0 if (famst==10 | famst==. | famst==12) /*alleinlebend*/
			replace iza_famstd = 1 if (famst==11 | famst==13) /*verheiratet/in haeusl. Gemeinschaft*/
			
			/*Single parent*/
			gen iza_allein_erz=1 if famst==12
			replace iza_allein_erz=0 if iza_allein_erz==.
			
			/*Number of children*/
			gen iza_kind = 0                               
			replace iza_kind = 1 if kind==1              
			replace iza_kind = 2 if kind>=2 & kind<=99 
	
			/*Nationality*/
			gen iza_staat = 1                                
			replace iza_staat = 0 if staat==0 | staat==.     /*German*/
	
			/*---Employment status before unemployment---*/
			gen iza_zug_gr = zug_gr
			replace iza_zug_gr = 1 if zug_gr==60 | zug_gr==62 | (zug_gr>=73 & zug_gr<=75) /*Beschaeftigung/sonst. Erwerb*/
			replace iza_zug_gr = 2 if zug_gr==61 /*Selbststaendige/Mithelfende*/
			replace iza_zug_gr = 3 if (zug_gr==63 | zug_gr==64 | zug_gr==69 | zug_gr==71 | zug_gr==76 | zug_gr==77) /*Schule/noch nie erwerbstaetig/Ausbildung*/
			replace iza_zug_gr = 4 if zug_gr==65 /*Arbeitunfaehig*/
			replace iza_zug_gr = 5 if zug_gr==68 /*Andere bereits einmal erwerbstaetig*/
			replace iza_zug_gr = 6 if (zug_gr==66 | zug_gr==67 | zug_gr==70 | zug_gr==72 | zug_gr==78) /*Sonstige*/
			replace iza_zug_gr = . if zug_gr==54 /*Keine Zuordnung moeglich*/
			
			
			/*Occupation of last employment before unemployment*/
			replace beruf=. if beruf>=999  /*wurde in umbenennung vergessen (Keine Angabe)*/
			gen beruf_kat = .
			replace beruf_kat =  1 if (beruf>=10 & beruf<=69) /* Pflanzenbauer, Tierzuehter, Fischereiberufe (01-06)*/
			replace beruf_kat =  2 if (beruf>=70 & beruf<=99) /* Bergleute, Mineralgewinner (07-09) */
			replace beruf_kat =  3 if (beruf >= 100 & beruf <= 119) /* Steinbearbeiter, Baustoffhersteller (10-11) */
			replace beruf_kat =  4 if (beruf >= 120 & beruf <= 139) /* Keramiker, Glasmacher (12-13) */
			replace beruf_kat =  5 if (beruf >= 140 & beruf <= 159) /* Chemiearbeiter, Kunststoffverarbeiter (14-15) */
			replace beruf_kat =  6 if (beruf >= 160 & beruf <= 179) /* Papierhersteller, -verarbeiter, Drucker (16-17) */
			replace beruf_kat =  7 if (beruf >= 180 & beruf <= 189) /* Holzaufbereiter, Holzwarenfertiger und verwandte Berufe (18) */
			replace beruf_kat =  8 if (beruf >= 190 & beruf <= 249) /* Metallerzeuger, -bearbeiter (19-24) */
			replace beruf_kat =  9 if (beruf >= 250 & beruf <= 309) /* Schlosser, Mechaniker und zugeordnete Berufe (25-30) */
			replace beruf_kat = 10 if (beruf >= 310 & beruf <= 319) /* Elektriker (31) */
			replace beruf_kat = 11 if (beruf >= 320 & beruf <= 329) /* Montierer und Metallberufe, a.n.g. (32) */
			replace beruf_kat = 12 if (beruf >= 330 & beruf <= 369) /* Textil- und Bekleidungsberufe (33-36) */
			replace beruf_kat = 13 if (beruf >= 370 & beruf <= 379) /* Lederhersteller, Leder- und Fellverarbeiter (37) */
			replace beruf_kat = 14 if (beruf >= 390 & beruf <= 439) /* Ernrungsberufe (39-43) */
			replace beruf_kat = 15 if (beruf >= 440 & beruf <= 479) /* Bauberufe (44-47) */
			replace beruf_kat = 16 if (beruf >= 480 & beruf <= 499) /* Bau-, Raumausstatter, Polsterer (48-49) */
			replace beruf_kat = 17 if (beruf >= 500 & beruf <= 509) /* Tischler, Modellbauer (50) */
			replace beruf_kat = 18 if (beruf >= 510 & beruf <= 519) /* Maler, Lackierer und verwandte Berufe (51) */
			replace beruf_kat = 19 if (beruf >= 520 & beruf <= 529) /* Warenpruefer, Versandfertigmacher (52) */
			replace beruf_kat = 20 if (beruf >= 530 & beruf <= 539) /* Hilfsarbeiter ohne n. Taetgkeitsangabe (53) */
			replace beruf_kat = 21 if (beruf >= 540 & beruf <= 549) /* Maschinisten und zugehige Berufe (54) */
			replace beruf_kat = 22 if (beruf >= 600 & beruf <= 619) /* Ingenieure, Chemiker, Physiker, Mathematiker (60-61) */
			replace beruf_kat = 23 if (beruf >= 620 & beruf <= 639) /* Techniker, Technische Sonderfachkrte (62-63) */
			replace beruf_kat = 24 if (beruf >= 680 & beruf <= 689) /* Warenkaufleute (68) */
			replace beruf_kat = 25 if (beruf >= 690 & beruf <= 709) /* Dienstleistungskaufleute und zugeh Berufe (69-70) */
			replace beruf_kat = 26 if (beruf >= 710 & beruf <= 749) /* Verkehrsberufe (71-74) */
			replace beruf_kat = 27 if (beruf >= 750 & beruf <= 789) /* Organisations-, Verwaltungs-, Boberufe (75-78) */
			/* Ordnungs-, Sicherheitsberufe (79-81) */
			replace beruf_kat = 28 if (beruf >= 791 & beruf <= 809) & beruf ~= 802 & beruf ~= 804 /* Dienst-, Wachberufe, Sicherheitswahrer (ohne Berufsfeuerwehr u. Schornsteinfeger) */
			replace beruf_kat = 29 if  beruf == 802                     /* Berufsfeuerwehr */ 
			replace beruf_kat = 30 if  beruf == 804                     /* Schornsteinfeger */ 
			replace beruf_kat = 31 if (beruf >= 810 & beruf <= 819) /* Rechtswahrer, -berater (81) */
			replace beruf_kat = 32 if (beruf >= 820 & beruf <= 839) /* Schriftwerksch., schriftwerkordnende sowie kuenstl. Berufe (82-83) */
			replace beruf_kat = 33 if (beruf >= 840 & beruf <= 859) /* Gesundheitsdienstberufe (84-85) */
			/* Sozial- und Erziehungsberufe, anderweitig nicht genannte geistes- und naturwissenschaftliche Berufe (86-89) */
			replace beruf_kat = 34 if (beruf >= 860 & beruf <= 869) /* Sozialpflegerische Berufe (86) */
			replace beruf_kat = 35 if (beruf >= 870 & beruf <= 879) /* Lehrer (87) */
			replace beruf_kat = 36 if (beruf >= 880 & beruf <= 888) /* Geistes- und naturwissenschaftliche Berufe (88) */
			replace beruf_kat = 37 if (beruf >= 890 & beruf <= 899) /* Seelsorger (89) */
			/* Allgemeine Dienstleistungsberufe (90-93) */
			replace beruf_kat = 38 if (beruf >= 900 & beruf <= 909) /* Koerperpfleger (90) */
			replace beruf_kat = 39 if (beruf >= 910 & beruf <= 919) /* Gaestebetreuer (91) */
			replace beruf_kat = 40 if (beruf >= 920 & beruf <= 929) /* Hauswirtschaftliche Berufe (92) */
			replace beruf_kat = 41 if (beruf >= 930 & beruf <= 939) /* Reinigungsberufe (93) */
			/* Sonstige Arbeitskraefte (97-99) */
			replace beruf_kat = 42 if (beruf >= 970 & beruf <= 979) /* Mithelfende Familienangehoerige (97) */
			replace beruf_kat = 43 if (beruf >= 980 & beruf <= 989) /* Arbeitskraefte (n. best. Berufe) (98) */
			replace beruf_kat = 44 if (beruf >= 990 & beruf <= 998) /* Arbeistk. ohne Taetigkeit inkl. Altersteilzeit, Ausgleichsgelr (99) */
			replace beruf_kat = 55 if  beruf == 555 /* Behinderte */
			replace beruf_kat = 66 if  beruf == 666 /* Rehabilitanden */
			replace beruf_kat = 88 if  beruf == 888 /* Pflegepersonen */
			/* Ende (beruf_kat)*/
			gen     iza_beruf_kat = .
			replace iza_beruf_kat = 1 if beruf_kat==1 /*Planzenbauer,Tierzuechter,Fischerei*/
			replace iza_beruf_kat = 3 if beruf_kat>=3 & beruf_kat<=21 /*Fertigungsberufe*/
			replace iza_beruf_kat = 4 if beruf_kat>=22 & beruf_kat<=23 /*Technische Berufe*/
			replace iza_beruf_kat = 5 if beruf_kat>=24 & beruf_kat<=41 /*Dienstleistungsberufe*/
			replace iza_beruf_kat = 6 if (beruf_kat>=42 & beruf_kat<=44) | beruf_kat==2 | beruf_kat==55 | beruf_kat==66 | beruf_kat==88 | beruf_kat==. /*Sonst. Arbeitskraefte(inkl.Bergleute, Mineralgewinner*/
			
			
			/*Gender*/
			gen iza_sex = frau        /*Women*/
			
			/*date of birth youngest child*/
			gen iza_alter_jkid = iza_eintritty - gebjahr_jkind
			
			/*Child < 3 years*/
			gen iza_kind_3 = 1 if iza_alter_jkid<3
			replace iza_kind_3 = 0 if iza_kind_3==.
			
			/*Child 3-5 years*/
			gen iza_kind_3bis5 = 1 if iza_alter_jkid>=3 & iza_alter_jkid<=5
			replace iza_kind_3bis5 = 0 if iza_kind_3bis5==.
			
			/*children < 10 years*/
			gen iza_kind_10 = 0
			replace iza_kind_10 = 1 if iza_kind > 0 & iza_alter_jkid < 10

			/*Reason termination last labour contract*/
			gen iza_ende_last = 0   /*Arbeitgeber, sonstiges*/
			replace iza_ende_last = 1 if art_kuend == 2 | art_kuend==3 /*Selber gekuendigt*/
			
			/*School degree*/
			gen iza_abschluss_kat     = 0 if schbild==1 | schbild==.    /*kein Schulabschluss*/
			replace iza_abschluss_kat = 1 if schbild==3                /*Hauptschulabschluss*/
			replace iza_abschluss_kat = 2 if schbild==5                /*Mittlere Reife*/
			replace iza_abschluss_kat = 3 if schbild==6                /*Fachhochschulreife*/
			replace iza_abschluss_kat = 4 if schbild==7                /*Abitur/Hochschulreife*/
			
			/*Occupational training*/
			gen iza_ausb_kat = 0 if ausbildung==21 | ausbildung==.  /*unbekannt*/
			replace iza_ausb_kat = 1 if ausbildung==22 /*Internal training*/
			replace iza_ausb_kat = 2 if ausbildung==23 /*External training*/
			replace iza_ausb_kat = 3 if (ausbildung==24|ausbildung==25|ausbildung==26|bild_beh==5) /*Technical college*/
			replace iza_ausb_kat = 4 if (ausbildung==27|bild_beh==6) /*University degree*/
			
			/*Number vacancy referrals*/
			gen iza_vermitt = anz_verm
			gen iza_berat = anz_beratung
			
			/*Qualification level*/
			gen iza_quali = quali
			replace iza_quali = 0 if quali==6  
			replace iza_quali = 0 if quali==12
			replace iza_quali = 0 if quali==. /*Kraefte mit und ohne Fachkenntnisse+Hilfskraefte*/
			replace iza_quali = 1 if quali==5                    /*Fachkraefte*/
			replace iza_quali = 2 if quali==4                    /*Kraefte mit Fachschulniveau*/
			replace iza_quali = 3 if quali==1
			replace iza_quali = 3 if quali==2
			replace iza_quali = 3 if quali==3 /*Top Management*/
			/*Berufserfahrung*/
			gen iza_beruferf = berufserfahrung
			replace iza_beruferf = 0 if berufserfahrung==3 /*ohne Berufserfahrung*/
			replace iza_beruferf = 1 if berufserfahrung==2 /*mit Berufserfahrung*/
			
			/*Daily earnings last employment*/
			by iza_id: gen iza_last_daily_income = tag_entg
		
			drop quelle tag_entg spell begepi endepi beg_orig end_orig beg_plan end_plan bet_nr frau schbild ausbildung stib erw_stat zug_gr
				
			saveold  $data/xvars_`year'_`iebversion', replace
			
		}/*year*/

}

log close
